#include "header/pyramid_torch.h"



NodePtr createTorch()
{
	Matrix matrix;
	//Load texture images
	ImagePtr torchHolderImage = Image::create();	 
	torchHolderImage->read("materials/metall.jpg");
	 
	ImagePtr torchImage = Image::create();
    torchImage->read("materials/torch_wood.jpg");
	
	// ** torchHolderRing **
	NodePtr torchHolderRing = makeTorus( 0.4, 1.6, 6, 12 );	
	TransformPtr ttorchHolderRing = Transform::create();
	beginEditCP(ttorchHolderRing, Transform::MatrixFieldMask);
		matrix.setIdentity();
		matrix.setTranslate(0, 0, 0);
		matrix.setRotate(Quaternion(Vec3f(1,0,0), +3.14159/2)); 
		ttorchHolderRing->setMatrix(matrix);
	endEditCP(ttorchHolderRing, Transform::MatrixFieldMask);
	
	NodePtr torchHolderRingTrans = Node::create();
	beginEditCP(torchHolderRingTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		torchHolderRingTrans->setCore(ttorchHolderRing);
		torchHolderRingTrans->addChild(torchHolderRing);
	endEditCP(torchHolderRingTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	

	// ** torchHolderRingCon **
	NodePtr torchHolderRingCon = makeCylinder( 2.8, 0.4, 8, true, true, true );
	TransformPtr ttorchHolderRingCon = Transform::create();
	beginEditCP(ttorchHolderRingCon, Transform::MatrixFieldMask);
		matrix.setIdentity();
		matrix.setTranslate(-3, 0, 0);
		matrix.setRotate(Quaternion(Vec3f(1,0,0), +3.14159/2)); 
		matrix.setRotate(Quaternion(Vec3f(0,0,1), +3.14159/2));
		ttorchHolderRingCon->setMatrix(matrix);
	endEditCP(ttorchHolderRingCon, Transform::MatrixFieldMask);
	
	NodePtr torchHolderRingConTrans = Node::create();
	beginEditCP(torchHolderRingConTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		torchHolderRingConTrans->setCore(ttorchHolderRingCon);
		torchHolderRingConTrans->addChild(torchHolderRingCon);
	endEditCP(torchHolderRingConTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	// ** torchHolderWall **
	NodePtr torchHolderWall = makeBox(0.5, 2.2, 2.2, 1, 1, 1);
	TransformPtr ttorchHolderWall = Transform::create();
	beginEditCP(ttorchHolderWall, Transform::MatrixFieldMask);
		matrix.setIdentity();
		matrix.setTranslate(-4.2, 0, 0);
		ttorchHolderWall->setMatrix(matrix);
	endEditCP(ttorchHolderWall, Transform::MatrixFieldMask);
	
	NodePtr torchHolderWallTrans = Node::create();
	beginEditCP(torchHolderWallTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		torchHolderWallTrans->setCore(ttorchHolderWall);
		torchHolderWallTrans->addChild(torchHolderWall);
	endEditCP(torchHolderWallTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);

	
	// ** torchHolderTorch **
	NodePtr torchHolderTorch = makeConicalFrustum(16, 1.5, 0.3, 15, 1, 1, 1);	
	TransformPtr ttorchHolderTorch = Transform::create();
	beginEditCP(ttorchHolderTorch, Transform::MatrixFieldMask);
		matrix.setIdentity();
		matrix.setTranslate(0, -3.8, 0); 
		ttorchHolderTorch->setMatrix(matrix);
	endEditCP(ttorchHolderTorch, Transform::MatrixFieldMask);
	
	NodePtr torchHolderTorchTrans = Node::create();
	beginEditCP(torchHolderTorchTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		torchHolderTorchTrans->setCore(ttorchHolderTorch);
		torchHolderTorchTrans->addChild(torchHolderTorch);
	endEditCP(torchHolderTorchTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	
	// ** torchHolderTorchTop **
	NodePtr torchHolderTorchTop = makeSphere(2,1.5);
	TransformPtr ttorchHolderTorchTop = Transform::create();
	beginEditCP(ttorchHolderTorchTop, Transform::MatrixFieldMask);
		matrix.setIdentity();
		matrix.setTranslate(0, 4.2, 0);
		ttorchHolderTorchTop->setMatrix(matrix);
	endEditCP(ttorchHolderTorchTop, Transform::MatrixFieldMask);
	
	NodePtr torchHolderTorchTopTrans = Node::create();
	beginEditCP(torchHolderTorchTopTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);
		torchHolderTorchTopTrans->setCore(ttorchHolderTorchTop);
		torchHolderTorchTopTrans->addChild(torchHolderTorchTop);
	endEditCP(torchHolderTorchTopTrans, Node::CoreFieldMask | Node::ChildrenFieldMask);

	/*****************************
	 * Textures torchHolder
	 * ***************************/
	 //Set texture options	     		
	 TextureChunkPtr torchHolderTex = TextureChunk::create();
		 beginEditCP(torchHolderTex);
		     torchHolderTex->setImage(torchHolderImage);
		     torchHolderTex->setMinFilter(GL_LINEAR);
		     torchHolderTex->setMagFilter(GL_LINEAR);
		     torchHolderTex->setWrapS(GL_REPEAT);
		     torchHolderTex->setWrapT(GL_REPEAT);
		     torchHolderTex->setEnvMode(GL_MODULATE);
	     endEditCP(torchHolderTex);
	 //Set material options    	
	 SimpleMaterialPtr torchHolderMat = SimpleMaterial::create();
	     beginEditCP(torchHolderMat);
		     torchHolderMat->setAmbient(Color3f(0.3, 0.3, 0.3)); 
		     torchHolderMat->setDiffuse(Color3f(1.0, 1.0, 1.0));
		     torchHolderMat->addChunk(torchHolderTex);
	     endEditCP(torchHolderMat);
	 
	 //Set the material on the torch holder
	 GeometryPtr torchHolderGeo = GeometryPtr::dcast(torchHolderRing->getCore());
	    beginEditCP(torchHolderGeo);
	     	torchHolderGeo->setMaterial(torchHolderMat);
	    endEditCP(torchHolderGeo);

     torchHolderGeo = GeometryPtr::dcast(torchHolderRingCon->getCore());
		  beginEditCP(torchHolderGeo);
		 	 torchHolderGeo->setMaterial(torchHolderMat);
		  endEditCP(torchHolderGeo);
	 	 
      torchHolderGeo = GeometryPtr::dcast(torchHolderWall->getCore());
		   beginEditCP(torchHolderGeo);
		 	  torchHolderGeo->setMaterial(torchHolderMat);
		   endEditCP(torchHolderGeo);

	/*********************
	 * Textures torch	
	 * *******************/
     
     //Set texture optons
     TextureChunkPtr torchTex = TextureChunk::create();
     	beginEditCP(torchTex);
     		torchTex->setImage(torchImage);
     		torchTex->setMinFilter(GL_LINEAR);
     		torchTex->setMagFilter(GL_LINEAR);
     		torchTex->setWrapS(GL_REPEAT);
     		torchTex->setWrapT(GL_REPEAT);
     		torchTex->setEnvMode(GL_MODULATE);
		endEditCP(torchTex);

	 //Set material options    	
	 SimpleMaterialPtr torchMat = SimpleMaterial::create();
	   	beginEditCP(torchMat);
		    torchMat->setAmbient(Color3f(0.3, 0.3, 0.3)); 
		    torchMat->setDiffuse(Color3f(1.0, 1.0, 1.0));
		    torchMat->addChunk(torchTex);
	    endEditCP(torchMat);
	  
	 //Set the material on the torch
	 GeometryPtr torchGeo = GeometryPtr::dcast(torchHolderTorch->getCore());
	    beginEditCP(torchGeo);
	    	torchGeo->setMaterial(torchMat);
	    endEditCP(torchGeo);
	    	
    		
	 torchGeo = GeometryPtr::dcast(torchHolderTorchTop->getCore());
	    beginEditCP(torchGeo);
	    	torchGeo->setMaterial(torchMat);
	    endEditCP(torchGeo);   	
	    	
	    	
	
	NodePtr torchComplete = Node::create();
	beginEditCP(torchComplete, Node::CoreFieldMask | Node::ChildrenFieldMask);
		torchComplete->setCore(Group::create());
		torchComplete->addChild(torchHolderRingTrans);
		torchComplete->addChild(torchHolderRingConTrans);
		torchComplete->addChild(torchHolderWallTrans);
		torchComplete->addChild(torchHolderTorchTrans);
		torchComplete->addChild(torchHolderTorchTopTrans);
	endEditCP(torchComplete, Node::CoreFieldMask | Node::ChildrenFieldMask);
	
	return torchComplete;
}
